package com.ruoyi.zyx.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.zyx.domain.ZyxAllot;
import com.ruoyi.zyx.domain.ZyxCheck;
import com.ruoyi.zyx.domain.ZyxTakecare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zyx.mapper.ZyxWarnMapper;
import com.ruoyi.zyx.domain.ZyxWarn;
import com.ruoyi.zyx.service.IZyxWarnService;

/**
 * 提醒信息Service业务层处理
 *
 * @author lhs
 * @date 2021-06-03
 */
@Service
public class ZyxWarnServiceImpl implements IZyxWarnService
{
    @Autowired
    private ZyxWarnMapper zyxWarnMapper;
    @Autowired
    private ZyxTakecareServiceImpl  zyxTakecareService;
    @Autowired
    private ZyxCheckServiceImpl  zyxCheckService;
    @Autowired
    private ZyxAllotServiceImpl zyxAllotService;
    /**
     * 查询提醒信息
     *
     * @param id 提醒信息ID
     * @return 提醒信息
     */
    @Override
    public ZyxWarn selectZyxWarnById(Long id)
    {
        return zyxWarnMapper.selectZyxWarnById(id);
    }

    @Override
    public int getMyWarnNum( Long deptid) {

        return zyxWarnMapper.getMyWarnNum(deptid);
    }

    /**
     * 查询提醒信息列表
     *
     * @param zyxWarn 提醒信息
     * @return 提醒信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxWarn> selectZyxWarnList(ZyxWarn zyxWarn)
    {
        return zyxWarnMapper.selectZyxWarnList(zyxWarn);
    }

    /**
     * 新增提醒信息
     *
     * @param zyxWarn 提醒信息
     * @return 结果
     */
    @Override
    public int insertZyxWarn(ZyxWarn zyxWarn)
    {
        zyxWarn.setCreateTime(DateUtils.getNowDate());
        return zyxWarnMapper.insertZyxWarn(zyxWarn);
    }

    /**
     * 修改提醒信息
     *
     * @param zyxWarn 提醒信息
     * @return 结果
     */
    @Override
    public int updateZyxWarn(ZyxWarn zyxWarn)
    {
        zyxWarn.setUpdateTime(DateUtils.getNowDate());
        return zyxWarnMapper.updateZyxWarn(zyxWarn);
    }

    @Override
    public int updMyWarnRead(Long id) {
        //同时更新设备下次的提醒日期
        ZyxWarn zyxWarn=zyxWarnMapper.selectZyxWarnById(id);
        if(zyxWarn.getWarntype().equals("1")){//保养
            ZyxTakecare zyxTakecare=zyxTakecareService.getByDeviceId(zyxWarn.getDeviceid());
            zyxTakecare.setIswarn("N");
            zyxTakecare.setNextdate(DateUtils.addDays(zyxTakecare.getNextdate(),(zyxTakecare.getWarncycle()-zyxTakecare.getWarndaynum())));
            zyxTakecareService.updateZyxTakecare(zyxTakecare);
        }else if(zyxWarn.getWarntype().equals("2")){//检定
            ZyxCheck zyxCheck=zyxCheckService.getByDeviceId(zyxWarn.getDeviceid());
            zyxCheck.setIswarn("N");
            zyxCheck.setNextdate(DateUtils.addDays(zyxCheck.getNextdate(),(zyxCheck.getWarncycle()-zyxCheck.getWarndaynum())));
            zyxCheckService.updateZyxCheck(zyxCheck);
        }else if(zyxWarn.getWarntype().equals("4")){//借调
            ZyxAllot zyxAllot=zyxAllotService.getByDeviceId(zyxWarn.getDeviceid());
//            ZyxCheck zyxCheck=zyxCheckService.getByDeviceId(zyxWarn.getDeviceid());
//            zyxAllot.setIswarn("N");
//            zyxAllot.setNextdate(DateUtils.addDays(zyxCheck.getNextdate(),(zyxCheck.getWarncycle()-zyxCheck.getWarndaynum())));
//            zyxCheckService.updateZyxCheck(zyxCheck);
        }
        zyxWarn.setIsread("Y");
        zyxWarn.setReaddate(new Date());
        zyxWarn.setReaduser(SecurityUtils.getLoginUser().getUser().getUserId());
        return zyxWarnMapper.updateZyxWarn(zyxWarn);
    }

    /**
     * 批量删除提醒信息
     *
     * @param ids 需要删除的提醒信息ID
     * @return 结果
     */
    @Override
    public int deleteZyxWarnByIds(Long[] ids)
    {
        return zyxWarnMapper.deleteZyxWarnByIds(ids);
    }

    /**
     * 删除提醒信息信息
     *
     * @param id 提醒信息ID
     * @return 结果
     */
    @Override
    public int deleteZyxWarnById(Long id)
    {
        return zyxWarnMapper.deleteZyxWarnById(id);
    }
}
